﻿
Partial Class Public_Register
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            Me.AddVolunteer1.InitData()
        End If
    End Sub

    Protected Sub AddVolunteer1_Saving(ByVal sender As Object, ByVal e As System.EventArgs) Handles AddVolunteer1.Saving

        If Me.SaveData() = True Then
            Me.MultiView1.ActiveViewIndex = 1
        End If
    End Sub
End Class

Partial Class Public_Register

    Private Function SaveData() As Boolean
        Dim dat As New clsData
        Dim sql As New MyStringBuilder
        Dim i As Integer

        'If Me.AddVolunteer1.VerificationCode <> Me.AddVolunteer1.VerificationCode Then
        '    MyObj.ShowMessage(Me.GetLocalResourceObject("SaveData_InvalidVerificationCode"))
        '    Return False
        'End If

        If My.CheckSame.CheckSent("Volunteer", "VolunteerID", "", "VolunteerID", Me.AddVolunteer1.VolunteerID) Then
            MyObj.ShowMessage(Me.GetLocalResourceObject("SameVolunteerID"))
            Return False
            'ElseIf Me.AddVolunteer1.MyVerificationCode <> Me.AddVolunteer1.VerificationCode Then
            '    MyObj.ShowMessage(Me.GetLocalResourceObject("SaveData_InvalidVerificationCode"))
            '    Return False
        End If

        Using scope As New Transactions.TransactionScope
            sql.Length = 0
            sql.AppendFormat("insert Volunteer (VolunteerID,EcolifeID,VolunteerName,Password,IdentificationID,IdentificationType,Active,RegisterDate,Gender,Birthday,MobilePhone,OfficePhone,BirthVillageID,BirthAddress,SameAddress,VillageID,Address,EmergentPerson,EmergentPhone,Email,EducationLevel,OccupationType,Retired,PhotoPath,PersonName) select ")
            sql.AppendFormat(" '{0}' ", Me.AddVolunteer1.VolunteerID)
            sql.AppendFormat(",'{0}' ", Me.AddVolunteer1.EcolifeID)
            sql.AppendFormat(",'{0}' ", Me.AddVolunteer1.VolunteerName)
            sql.AppendFormat(",'{0}' ", clsAuth.EncryptPassword(Me.AddVolunteer1.VolunteerPassword))
            sql.AppendFormat(",'{0}' ", Me.AddVolunteer1.VolunteerID)
            sql.AppendFormat(",'{0}' ", Me.AddVolunteer1.IdentificationType)
            sql.AppendFormat(",'{0}' ", "1")
            sql.AppendFormat(",{0} ", "getdate()")
            sql.AppendFormat(",'{0}' ", Me.AddVolunteer1.Gender)
            sql.AppendFormat(",'{0:yyyy/MM/dd}' ", Me.AddVolunteer1.Birthday)
            sql.AppendFormat(",'{0}' ", Me.AddVolunteer1.MobilePhone)
            sql.AppendFormat(",'{0}' ", Me.AddVolunteer1.OfficePhone)
            sql.AppendFormat(",'{0}' ", Me.AddVolunteer1.BirthRegionID)
            sql.AppendFormat(",'{0}' ", Me.AddVolunteer1.BirthAddress)
            sql.AppendFormat(",{0} ", Me.AddVolunteer1.SameAddress)
            sql.AppendFormat(",'{0}' ", Me.AddVolunteer1.RegionID)
            sql.AppendFormat(",'{0}' ", Me.AddVolunteer1.Address)
            sql.AppendFormat(",'{0}' ", Me.AddVolunteer1.EmergentPerson)
            sql.AppendFormat(",'{0}' ", Me.AddVolunteer1.EmergentPhone)
            sql.AppendFormat(",'{0}' ", Me.AddVolunteer1.Email)
            sql.AppendFormat(",'{0}' ", Me.AddVolunteer1.EducationLevel)
            sql.AppendFormat(",'{0}' ", Me.AddVolunteer1.OccupationType)
            sql.AppendFormat(",'{0}' ", Me.AddVolunteer1.Retired)
            sql.AppendFormat(",'{0}.jpg' ", Me.AddVolunteer1.VolunteerID)
            sql.AppendFormat(",'{0}' ", Me.AddVolunteer1.PersonName)

            dat.AccData(sql.ToString(), Nothing, Data.CommandType.Text)

            sql.Length = 0
            sql.AppendFormat("insert VolunteerApplication (VolunteerID,CreateDate,Status) select ")
            sql.AppendFormat(" '{0}' ", Me.AddVolunteer1.VolunteerID)
            sql.AppendFormat(",{0} ", "getdate()")
            sql.AppendFormat(",'{0}' ", My.FormStatus.Approving)
            dat.AccData(sql.ToString(), Nothing, Data.CommandType.Text)

            sql.Length = 0
            Dim CheckCount As Integer = 0
            For i = 0 To Me.AddVolunteer1.WishOrganization.Length - 1
                If (Me.AddVolunteer1.WishOrganization(i) <> "") Then
                    sql.AppendFormat("insert WishOrganization (VolunteerID,OrganizationID) select ")
                    sql.AppendFormat(" '{0}' ", Me.AddVolunteer1.VolunteerID)
                    sql.AppendFormat(",'{0}' ", Me.AddVolunteer1.WishOrganization(i).ToString())
                    CheckCount += 1
                End If
            Next
            If CheckCount > MyObj.GetParameter("Register", "WishOrganizationCount") Then
                MyObj.ShowMessage(Me.GetLocalResourceObject("OverWishOrganization"))
                Return False
            End If

            dat.AccData(sql.ToString(), Nothing, Data.CommandType.Text)

            sql.Length = 0
            sql.AppendFormat("delete VolunteerWorkTime ")
            sql.AppendFormat("where VolunteerID = '{0}' ", Me.AddVolunteer1.VolunteerID)
            dat.AccData(sql)

            '可服務時間
            sql.Length = 0
            For i = 0 To 2
                sql.AppendFormat("insert VolunteerWorkTime select ")
                sql.AppendFormat(" '{0}' ", Me.AddVolunteer1.VolunteerID)
                Select Case i
                    Case 0
                        sql.AppendFormat(",'{0}' ", "A")
                    Case 1
                        sql.AppendFormat(",'{0}' ", "N")
                    Case 2
                        sql.AppendFormat(",'{0}' ", "P")
                    Case Else
                        sql.AppendFormat(",'{0}' ", "A")
                End Select
                sql.AppendFormat(",'{0}' ", Me.AddVolunteer1.WorkTime(i, 0))
                sql.AppendFormat(",'{0}' ", Me.AddVolunteer1.WorkTime(i, 1))
                sql.AppendFormat(",'{0}' ", Me.AddVolunteer1.WorkTime(i, 2))
                sql.AppendFormat(",'{0}' ", Me.AddVolunteer1.WorkTime(i, 3))
                sql.AppendFormat(",'{0}' ", Me.AddVolunteer1.WorkTime(i, 4))
                sql.AppendFormat(",'{0}' ", Me.AddVolunteer1.WorkTime(i, 5))
                sql.AppendFormat(",'{0}' ", Me.AddVolunteer1.WorkTime(i, 6))
            Next
            dat.AccData(sql.ToString(), Nothing, Data.CommandType.Text)

            '訓練時數
            sql.Length = 0
            For i = 0 To 2
                sql.AppendFormat("insert OtherTrainingRecord (VolunteerID,TrainingHour,TrainingType,CreateBy,CreateDate,Active) select ")
                sql.AppendFormat(" '{0}' ", Me.AddVolunteer1.VolunteerID)
                sql.AppendFormat(" ,'{0}' ", Me.AddVolunteer1.TrainingHour(i))
                Select Case i
                    Case 0
                        sql.AppendFormat(",'{0}' ", "B")
                    Case 1
                        sql.AppendFormat(",'{0}' ", "S")
                    Case 2
                        sql.AppendFormat(",'{0}' ", "W")
                    Case Else
                        sql.AppendFormat(",'{0}' ", "B")
                End Select
                sql.AppendFormat(",'{0}' ", Me.AddVolunteer1.VolunteerID)
                sql.AppendFormat(",{0} ", "getdate()")
                sql.AppendFormat(",'N'")
            Next
            dat.AccData(sql.ToString(), Nothing, Data.CommandType.Text)

            '已服務時數累計           
            sql.Length = 0
            sql.AppendFormat("insert OtherWorkRecord (VolunteerID,WorkHour,WorkTimes,CreateDate,CreateBy,Active) select ")
            sql.AppendFormat(" '{0}' ", Me.AddVolunteer1.VolunteerID)
            sql.AppendFormat(",{0} ", Me.AddVolunteer1.OtherWorkHour)
            sql.AppendFormat(",1")
            sql.AppendFormat(",{0} ", "getdate()")
            sql.AppendFormat(",'{0}' ", Me.AddVolunteer1.VolunteerID)
            sql.AppendFormat(",'N'")
            'MyObj.WriteLog(sql.ToString())
            dat.AccData(sql.ToString(), Nothing, Data.CommandType.Text)

            scope.Complete()
        End Using

        Return True
    End Function
End Class